package com.hrma.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrma.dao.EmployeeMapper;
import com.hrma.dao.TravelApplicationMapper;
import com.hrma.entity.SelectResult;
import com.hrma.entity.TravelApplication;
import com.hrma.entity.TravelApplicationDTO;
import com.hrma.entity.TravelApplicationVO;
import com.hrma.entity.exception.StateException;
import com.hrma.services.TravelApplicationService;
import com.hrma.utils.UserUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TravelApplicationImpl implements TravelApplicationService {
    private final TravelApplicationMapper travelApplicationMapper;
    private final EmployeeMapper employeeMapper;

    public TravelApplicationImpl(TravelApplicationMapper travelApplicationMapper, EmployeeMapper employeeMapper) {
        this.travelApplicationMapper = travelApplicationMapper;
        this.employeeMapper = employeeMapper;
    }

    @Override
    public TravelApplication add(TravelApplication data) {
//        if(employeeMapper.getstate(UserUtil.getUserId()) == 0){
//            throw new StateException("员工已离职！");
//        }
        data.setEmployeeId(UserUtil.getUserId());
        travelApplicationMapper.add(data);
        return data;
    }

    @Override
    public void update(TravelApplication data) {
        if(!travelApplicationMapper.exist(data.getId())){
            throw new RuntimeException("id字段不存在！");
        }
        if(travelApplicationMapper.getstate(data.getId()) != 0){
            throw new StateException("当前申请状态不能进行修改！");
        }
        travelApplicationMapper.updata(data);
    }

    @Override
    public List<SelectResult> select() {
        return travelApplicationMapper.select();
    }

    @Override
    public Page<TravelApplicationVO> getList(TravelApplicationDTO data) {
        data.setState(0);
        PageHelper.startPage(data.getPageNum(),data.getPageSize());
        return travelApplicationMapper.getList(data);
    }

    @Override
    public Page<TravelApplicationVO> getPersonalList(TravelApplicationDTO data) {
        data.setState(0);
        data.setEmployeeId(UserUtil.getUserId());
        PageHelper.startPage(data.getPageNum(),data.getPageSize());
        return travelApplicationMapper.getList(data);
    }
}
